#include "Properties.h"
#include "io/FileInputStream.h"
#include "util/Util.h"
#include "array/Array.hpp"
#include "lang/String.h"
using namespace yzrilyzr_io;
using namespace yzrilyzr_util;
using namespace yzrilyzr_array;
using namespace yzrilyzr_lang;
namespace yzrilyzr_util{
	Properties::Properties(const File & file){
		if(!file.exists() || !file.isFile())return;
		FileInputStream stream(file);
		readStream(stream);
	}
	Properties::Properties(InputStream & stream){
		readStream(stream);
	}
	Properties::Properties(const String & str){
		load(str);
	}
	void Properties::readStream(InputStream & stream){
		auto array=Util::read(stream);
		String sb(array, UTF8);
		load(sb);
	}
	void Properties::load(const String & str){
		auto lines=str.split("\n");
		for(String & line : lines){
			if(line.startsWith("#")) continue;
			u_index eqPos=line.indexOf("=");
			if(eqPos != -1){
				auto key=line.substring(0, eqPos).trim();
				auto value=line.substring(eqPos + 1).trim();
				dict[key]=value;
			}
		}
	}

	String Properties::get(const String & key)const{
		auto it=dict.find(key);
		if(it != dict.end()){
			return it->second;
		}
		return key;
	}
}